package edu.nf.vac.api.advice;

import edu.nf.vac.api.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author LQJ
 * @date 2022/3/30
 * 全局的异常处理
 */
@Slf4j
@ControllerAdvice("edu.nf.vac.api.controller")
public class GlobalExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    public @ResponseBody
    ResultVO handleArithmeticException(ArithmeticException e){
        log.error(e.getMessage(),e);
        return response(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
    }
    @ExceptionHandler(RuntimeException.class)
    public @ResponseBody ResultVO handleRuntimeException(RuntimeException e){
        log.error(e.getMessage(),e);
        return response(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }
    private ResultVO response(int code, String message){
        ResultVO vo = new ResultVO();
        vo.setCode(code);
        vo.setMessage(message);
        return vo;
    }
}
